﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace IRC.IRepairClouds.WebClient.Controls
{
    public abstract class ScriptControl : System.Web.UI.UserControl, IScriptControl
    {
        public abstract IEnumerable<ScriptDescriptor> GetScriptDescriptors();

        public abstract IEnumerable<ScriptReference> GetScriptReferences();

        private ScriptManager _scriptManager;
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!this.DesignMode) {
                _scriptManager = ScriptManager.GetCurrent(Page);
                if (_scriptManager == null)
                    throw new HttpException("A ScriptManager control must exist on the current page.");
                _scriptManager.RegisterScriptControl(this);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
                _scriptManager.RegisterScriptDescriptors(this);
            base.Render(writer);
        }
    }
}
